/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007 University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef LAYER2_P2MP_CHANNEL_H
#define LAYER2_P2MP_CHANNEL_H

#include <list>
#include <vector>
#include "ns3/channel.h"
#include "ns3/ptr.h"
#include "ns3/nstime.h"
#include "ns3/data-rate.h"
#include "ns3/traced-callback.h"
#include "ns3/net-device-container.h"

namespace ns3 {

class Layer2P2PNetDevice;
class Packet;

/**
 * \ingroup layer2-p2mp
 * \brief Simple Directional Point To Multi Point Channel.(The multi point ranges from 1 to MAX_DST)
 *
 * This class represents a very simple point to multi point channel and has the capability of broadcasting.
 * There is a state (IDLE, TRANSMITTING) associated with each wire.
 *
 * \see Attach
 * \see TransmitStart
 */
class Layer2P2MPChannel : public Channel
{
public:
  /**
   * \brief Get the TypeId
   *
   * \return The TypeId for this class
   */
  static TypeId GetTypeId (void);

  /**
   * \brief Create a Layer2P2PChannel
   *
   * By default, you get a channel that has an "infinitely" fast 
   * transmission speed and zero delay.
   */
  Layer2P2MPChannel ();

  /**
   * \brief Attach a given netdevice to the src netdevice of this channel
   * \param device pointer to the netdevice to attach to the channel
   */
  void Attach (Ptr<Layer2P2PNetDevice> device);
  
  /**
   * \brief set a given dst netdevices to this channel
   * \param device container, the channel can commuinicate with these devices, enabled by the m_mask
   */
  void SetDstDevices (NetDeviceContainer devices);

  //-------------------------Circuit Switching----------------------------------------------
  /**
   * \brief enable the dest netdevices by setting m_mask
   * \param device container, the channel can commuinicate with these devices, enabled by the m_mask
   */
  void AddMask (uint16_t location);

  /**
   * \brief Get the Mask
   */
  std::vector<uint16_t> GetMask ();

  /**
   * \brief Clear the Mask
   */
  void ClearMask ();

  /**
   * \brief Get number of devices on this channel
   * \returns number of devices on this channel
   */
  virtual uint32_t GetNDevices (void) const;

  /**
   * \brief Get the source net-device 
   * \param i Index number of the device requested
   * \returns Ptr to NetDevice requested
   */
  virtual Ptr<NetDevice> GetDevice (uint32_t i) const;

    /**
   * \brief Get the dest net-device 
   * \param location Index number of the device requested
   * \returns Ptr to NetDevice requested
   */
  Ptr<NetDevice> GetDstDevice (uint32_t location);
  //---------------------------------------------------------------------------------------

  /**
   * \brief Transmit a packet over this channel
   * \param p Packet to transmit
   * \param src Source Layer2P2PNetDevice
   * \param txTime Transmit time to apply
   * \returns true if successful (currently always true)
   */
  virtual bool TransmitStart (Ptr<Packet> p, Ptr<Layer2P2PNetDevice> src, Time txTime);

protected:
  /**
   * \brief Get the delay associated with this channel
   * \returns Time delay
   */
  Time GetDelay (void) const;

  /**
   * \brief Check to make sure the link is initialized
   * \returns true if initialized, asserts otherwise
   */
  bool IsInitialized (void) const;
                    
private:
  /** \brief Wire states
     *
     */
    enum WireState
    {
      /** Initializing state */
      INITIALIZING,
      /** Idle state (no transmission from NetDevice) */
      IDLE,
      /** Transmitting state (data being transmitted from NetDevice. */
      TRANSMITTING,
      /** Propagating state (data is being propagated in the channel. */
      PROPAGATING
    };
  
  Time                    m_delay;              //!< Propagation Delay
  Time                    m_reconfigureDelay;   //!< Circuit Reconfigure Delay.
  WireState               m_state;              //!< State of the link
  Ptr<Layer2P2PNetDevice> m_src;                //!< Src NetDevice
  std::vector<Ptr<Layer2P2PNetDevice> > m_dst;  //!< Dest NetDevices smart pointers
  std::vector<uint16_t>                 m_mask; //!< Indicating the enabled dst netdevices, multi-netdevices means pysical layer multicast
};

} // namespace ns3

#endif /* POINT_TO_POINT_CHANNEL_H */
